﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmScienceManagementList : DockContent
    {
        BranchClient _wcfBranch;
        DepartmentClient _wcfDepartment;
        ScienceClient _wcfScience;
        MainServiceClient wcfMain;
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int _ScienceID = 0;
        int branch_ID;
        int department_ID;
        bool _loadedData = false;
        RoleRightInfo listRole;
        public frmScienceManagementList()
        {
            InitializeComponent();
            LoadLanguage();
            _wcfBranch = Service.Branch();
            _wcfDepartment = Service.Department();
            _wcfScience = Service.Science();
            wcfMain = Service.MainService();

            BuildVoucherGrid();
            UISetting.LoadTreeView3(ref tvBranch);
            UISetting.LoadMonths(ref cboMonth);

            cboMonth.ComboBox.SelectedValue = Convert.ToUInt32(DateTime.Now.Month);

            UISetting.LoadYears(ref cboYear);
            //cboYear.ComboBox.SelectedValue = Convert.ToUInt32(DateTime.Now.Year);

            listRole = UIParamater.Quyen_CacDeTaiKhoaHoc;
            if (!listRole.Update)
            {
                this.btnEdit.Image = Properties.Resources.app;
                this.btnEdit.Tag = "xem";
            }


            LoadData();
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.TabText = this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }

        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "Science_ID", "Mã chính", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "RowNumber", UpdateUI.GetString("stt"), "C", 50, true, true);
            UISetting.AddColumn(ref dgvList, "FullName", UpdateUI.GetString("nguoithuchien"), "", 130, true, true);
            UISetting.AddColumn(ref dgvList, "Science_TakePartAsName", UpdateUI.GetString("chucdanhthamgia"), "", 150, true, true);
            UISetting.AddColumn(ref dgvList, "Science_Name", UpdateUI.GetString("tendetai"), "", 150, true, true);
            UISetting.AddColumn(ref dgvList, "Science_Tenor", UpdateUI.GetString("noidung"), "", 300, true, true);
            UISetting.AddColumn(ref dgvList, "Science_FromDate", UpdateUI.GetString("tungay"), "", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Science_ToDate", UpdateUI.GetString("denngay"), "", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Science_ResultDate", UpdateUI.GetString("ngaynghiemthu"), "", 130, true, true);
            UISetting.AddColumn(ref dgvList, "Science_Result", UpdateUI.GetString("ketqua"), "", 100, true, true);
        }
        private void LoadData()
        {
            int _month = Convert.ToInt32(cboMonth.ComboBox.SelectedValue);
            int _year = Convert.ToInt32(cboYear.Text);

            DataSet _ds = _wcfScience.Science_GetList(out itemCount, currPage, pageSize, branch_ID, department_ID, _month, _year);
            PageIndex();
            dgvList.Rows.Clear();
            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["Science_ID"].Value = _ds.Tables[0].Rows[i]["Science_ID"].ToString();
                dgvList.Rows[i].Cells["RowNumber"].Value = _ds.Tables[0].Rows[i]["RowNumber"].ToString();
                dgvList.Rows[i].Cells["FullName"].Value = _ds.Tables[0].Rows[i]["Employee_LastName"].ToString() +
                    " " + _ds.Tables[0].Rows[i]["Employee_FirstName"].ToString();

                dgvList.Rows[i].Cells["Science_TakePartAsName"].Value = _ds.Tables[0].Rows[i]["Science_TakePartAsName"].ToString();
                dgvList.Rows[i].Cells["Science_Name"].Value = _ds.Tables[0].Rows[i]["Science_Name"].ToString();
                dgvList.Rows[i].Cells["Science_Tenor"].Value = _ds.Tables[0].Rows[i]["Science_Tenor"].ToString();
                dgvList.Rows[i].Cells["Science_FromDate"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Science_FromDate"]).ToString("dd/MM/yyyy");
                dgvList.Rows[i].Cells["Science_ToDate"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Science_ToDate"]).ToString("dd/MM/yyyy");
                if (_ds.Tables[0].Rows[i]["Science_ResultDate"] != DBNull.Value)
                    dgvList.Rows[i].Cells["Science_ResultDate"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Science_ResultDate"]).ToString("dd/MM/yyyy");
                dgvList.Rows[i].Cells["Science_Result"].Value = _ds.Tables[0].Rows[i]["Science_Result"].ToString();
            }
            _loadedData = true;
        }


        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            Form f = new frmScienceManagementInfo(0, listRole);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    _ScienceID = Convert.ToInt32(dgvList.SelectedRows[0].Cells["Science_ID"].Value);
                    Form f = new frmScienceManagementInfo(_ScienceID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _ScienceID = Convert.ToInt32(dgvList.CurrentRow.Cells["Science_ID"].Value);
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (_wcfScience.Science_Delete(_ScienceID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();

        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                btnEdit_Click(null, null);
        }

        private void frmScienceManagementList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDel_Click(null, null);
                    break;
            }
        }

        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            // txtKeyWord.Text = "";
            if (e.Node.Level == 0)
            {
                branch_ID = 0;
                department_ID = 0;
                currPage = 0;
                LoadData();
            }
            else if (e.Node.Level == 1)
            {
                branch_ID = Convert.ToInt32(e.Node.Tag);
                department_ID = 0;
                currPage = 0;
                LoadData();
            }
            else if (e.Node.Level >= 2)
            {
                department_ID = Convert.ToInt32(e.Node.Tag);
                string _sql = "Select Department_BranchID from IBMS_Department where Department_ID = " + department_ID.ToString();
                DataTable dt = wcfMain.Query(_sql).Tables[0];
                branch_ID = Convert.ToInt32(dt.Rows[0]["Department_BranchID"]);
                currPage = 0;
                LoadData();

            }

        }

        private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_loadedData)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (_loadedData)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void frmScienceManagementList_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
            dgvList.Columns["RowNumber"].HeaderText = UpdateUI.GetString("stt");
            dgvList.Columns["FullName"].HeaderText = UpdateUI.GetString("nguoithuchien");
            dgvList.Columns["Science_TakePartAsName"].HeaderText = UpdateUI.GetString("chucdanhthamgia");
            dgvList.Columns["Science_Name"].HeaderText = UpdateUI.GetString("tendetai");
            dgvList.Columns["Science_Tenor"].HeaderText = UpdateUI.GetString("noidung");
            dgvList.Columns["Science_FromDate"].HeaderText = UpdateUI.GetString("tungay");
            dgvList.Columns["Science_ToDate"].HeaderText = UpdateUI.GetString("denngay");
            dgvList.Columns["Science_ResultDate"].HeaderText = UpdateUI.GetString("ngaynghiemthu");
            dgvList.Columns["Science_Result"].HeaderText = UpdateUI.GetString("ketqua");
        }

        private void frmScienceManagementList_FormClosing(object sender, FormClosingEventArgs e)
        {
            _wcfBranch.Close();
            _wcfDepartment.Close();
            _wcfScience.Close();
            wcfMain.Close(); 

        }
    }
}
